package com.ibm.spatiotemp.server.mocking;

import org.apache.log4j.Logger;

import com.ibm.spatiotemp.server.constants.Constant;
import com.ibm.spatiotemp.server.datamodel.tenant.Tenant;
import com.ibm.spatiotemp.server.datamodel.tenant.TenantManager;
import com.ibm.spatiotemp.server.datasources.DataSchema;
import com.ibm.spatiotemp.server.datasources.DataSource;

public class TestTenantFactory {

	public static Logger logger = Logger.getLogger(TestTenantFactory.class);
	
	public static DataSchema fakeschema;
	public static Tenant faketenant;
	
	public static DataSchema mockschema;
	public static Tenant mocktenant;

	public static DataSchema mysqlchema;
	public static Tenant mysqlenant;
	
	public static DataSchema ifxschema;
	public static Tenant ifxtenant;
	public static Tenant ifxtenant2;
	
	public static DataSchema dynamodbchema;
	public static Tenant dynamodbtenant;
	
	
	protected static DataSchema getFakeDataSchema()
	{
		if(fakeschema == null){
			fakeschema = new DataSchema();
			fakeschema.setTab_name("faketab");

			fakeschema.setTs_colname("time");
			fakeschema.setX_colname("lontitue");
			fakeschema.setY_colname("latitue");
			fakeschema.setMovobj_colname("fakemodid");
			
			fakeschema.setTs_coltype("timestamp");
			fakeschema.setX_coltype("double");
			fakeschema.setY_coltype("double");
			fakeschema.setMovobj_coltype("string");
		}
		return fakeschema;
	}

	protected static DataSchema getMockDataSchema()
	{
		if(mockschema == null){
			mockschema = new DataSchema();
			mockschema.setTab_name("mocktable");

			mockschema.setTs_colname("time");
			mockschema.setX_colname("lontitue");
			mockschema.setY_colname("latitue");
			mockschema.setMovobj_colname("modid");
			
			mockschema.setTs_coltype("timestamp");
			mockschema.setX_coltype("double");
			mockschema.setY_coltype("double");
			mockschema.setMovobj_coltype("string");
		}
		return mockschema;
	}
	
	protected static DataSchema getMysqlDataSchema()
	{
		if(mysqlchema == null){
			mysqlchema = new DataSchema();
			mysqlchema.setTab_name("mysqltable");

			mysqlchema.setTs_colname("time");
			mysqlchema.setX_colname("lontitue");
			mysqlchema.setY_colname("latitue");
			mysqlchema.setMovobj_colname("modid");
			
			mysqlchema.setTs_coltype("timestamp");
			mysqlchema.setX_coltype("double");
			mysqlchema.setY_coltype("double");
			mysqlchema.setMovobj_coltype("string");
		}
		return mysqlchema;
	}
	
	protected static DataSchema getDynamodbDataSchema()
	{
		if(dynamodbchema == null){
			dynamodbchema = new DataSchema();
			dynamodbchema.setTab_name("dynamodb_table");

			dynamodbchema.setTs_colname("time");
			dynamodbchema.setX_colname("lontitue");
			dynamodbchema.setY_colname("latitue");
			dynamodbchema.setMovobj_colname("modid");
			
			dynamodbchema.setTs_coltype("timestamp");
			dynamodbchema.setX_coltype("double");
			dynamodbchema.setY_coltype("double");
			dynamodbchema.setMovobj_coltype("string");
		}
		return dynamodbchema;
	}

	protected static DataSchema getIfxDataSchema()
	{
		if(ifxschema == null){
			ifxschema = new DataSchema();
			ifxschema.setTab_name("vt_vehicle");

			ifxschema.setTs_colname("t");
			ifxschema.setX_colname("x");
			ifxschema.setY_colname("y");
			ifxschema.setMovobj_colname("modid");
			
			ifxschema.setTs_coltype("timestamp");
			ifxschema.setX_coltype("double");
			ifxschema.setY_coltype("double");
			ifxschema.setMovobj_coltype("string");
			ifxschema.setOther("\'calendar(cal_1s),origin(2008-01-01 00:00:00),threshold(0),container(c_tsbson),irregular\'");
		}
		return ifxschema;
	}
	
	protected static Tenant getFakeTenant()
	{
		if(faketenant!=null)
			return faketenant;
		
		faketenant = new Tenant();
		faketenant.setTenantid("faketenant");
		
		DataSource fakesource = new DataSource();
		fakesource.setAccessmode(accessmode);
		fakesource.setConnection_string("");
		fakesource.setDataschema(getFakeDataSchema());
		fakesource.setSrcname("fakesrc");
		fakesource.setSource_class(Constant.DB_CLASS_FAKE);
		
		faketenant.upsetDataSource(fakesource);
		return faketenant;
	}

	public static boolean hsql_memmode = true;
	public static int accessmode = Constant.DBACCESSMODE_IDXONLY;
	public static Tenant getMockingTenant()
	{
		if(mocktenant!=null)
			return mocktenant;
		
		mocktenant = new Tenant();
		mocktenant.setTenantid("mocktenant");
		
		DataSource mocksource = new DataSource();
		mocksource.setAccessmode(accessmode);
		
		if(hsql_memmode)
			mocksource.setConnection_string(Constant.DB_CLASS_MOCK_CONSTRING_MEM);
		else
			mocksource.setConnection_string(Constant.DB_CLASS_MOCK_CONSTRING_FILE);
		
		mocksource.setDataschema(getMockDataSchema());
		mocksource.setSrcname("mocksrc");
		mocksource.setSource_class(Constant.DB_CLASS_MOCK);
		
		mocktenant.upsetDataSource(mocksource);
		return mocktenant;
	}

	public static Tenant getMysqlTenant()
	{
		if(mysqlenant!=null)
			return mysqlenant;
		
		mysqlenant = new Tenant();
		mysqlenant.setTenantid("mysqltenant");
		
		DataSource mysqlsource = new DataSource();
		mysqlsource.setAccessmode(accessmode);
		
		String connstring = "jdbc:mysql://192.168.1.58:3307/test?user=root&password=root&autoReconnect=true&failOverReadOnly=false";
		mysqlsource.setConnection_string(connstring);
		
		mysqlsource.setDataschema(getMysqlDataSchema());
		mysqlsource.setSrcname("mysqlsrc");
		mysqlsource.setSource_class(Constant.DB_CLASS_MYSQL);
		
		mysqlenant.upsetDataSource(mysqlsource);
		return mysqlenant;
	}
	
	public static Tenant getDynamodbTenant()
	{
		if(dynamodbtenant!=null)
			return dynamodbtenant;
		
		dynamodbtenant = new Tenant();
		dynamodbtenant.setTenantid("dynamodbtenant");
		
		DataSource dynamodbsource = new DataSource();
		dynamodbsource.setAccessmode(accessmode);
		
		String connstring = "AWSCREDENTIAL:";
		dynamodbsource.setConnection_string(connstring);
		
		dynamodbsource.setDataschema(getDynamodbDataSchema());
		dynamodbsource.setSrcname("dynamodbsrc");
		dynamodbsource.setSource_class(Constant.DB_CLASS_DYNAMODB);
		
		dynamodbtenant.upsetDataSource(dynamodbsource);
		return dynamodbtenant;
	}
	
	protected static Tenant getIfxTenant()
	{
		if(ifxtenant!=null)
			return ifxtenant;
		
		ifxtenant = new Tenant();
		ifxtenant.setTenantid("tenant1");
		
		DataSource ifxsource = new DataSource();
		ifxsource.setAccessmode(0);
		String jdbcurl = "jdbc:informix-sqli://9.186.89.116:14582/dbtenant2:INFORMIXSERVER=ol_informix1210;USER=informix;PASSWORD=passw0rd";
		//jdbc:informix-sqli://9.186.89.57:10570/dbtenant1:INFORMIXSERVER=ol_informix1210;USER=informix;PASSWORD=passw0rd
		ifxsource.setConnection_string(jdbcurl);
		ifxsource.setDataschema(getIfxDataSchema());
		ifxsource.setSrcname("ifxsrc");
		ifxsource.setSource_class(Constant.DB_CLASS_IFX);
		
		ifxtenant.upsetDataSource(ifxsource);
		return ifxtenant;
	}
	
	protected static Tenant getIfxTenant2()
	{
		if(ifxtenant2!=null)
			return ifxtenant2;
		
		ifxtenant2 = new Tenant();
		ifxtenant2.setTenantid("tenant2");
		
		DataSource ifxsource = new DataSource();
		ifxsource.setAccessmode(0);
		String jdbcurl = "jdbc:informix-sqli://9.186.89.116:14582/dbtenant2:INFORMIXSERVER=ol_informix1210;USER=informix;PASSWORD=passw0rd";
		//jdbc:informix-sqli://9.186.89.57:10570/dbtenant2:INFORMIXSERVER=ol_informix1210;USER=informix;PASSWORD=passw0rd
		ifxsource.setConnection_string(jdbcurl);
		ifxsource.setDataschema(getIfxDataSchema());
		ifxsource.setSrcname("ifxsrc");
		ifxsource.setSource_class(Constant.DB_CLASS_IFX);
		
		ifxtenant2.upsetDataSource(ifxsource);
		return ifxtenant2;
	}
	
	public static void initTenantManager()
	{
		TenantManager.upsetTenant(getMockingTenant());
		TenantManager.upsetTenant(getFakeTenant());
		TenantManager.upsetTenant(getIfxTenant());
		TenantManager.upsetTenant(getIfxTenant2());
		TenantManager.upsetTenant(getMysqlTenant());
		TenantManager.upsetTenant(getDynamodbTenant());
	}
	
	public static void main(String[] args)
	{
		initTenantManager();
		
		String mockschema_str = mockschema.toJsonFormat();
		logger.info(
				mockschema_str
		);
		DataSchema mockschema_mirror = new DataSchema();
		mockschema_mirror.buildInstanceByJsonString(mockschema_str);

		logger.info(
				mockschema_mirror.toJsonFormat()
		);
		
		DataSource mocksource = getMockingTenant().getDataSourceBySourceName("mocksrc");
		String mocksource_str = mocksource.toJsonFormat();
		logger.info(
				mocksource_str
		);
		DataSource mocksource_mirror = new DataSource();
		mocksource_mirror.buildInstanceByJsonString(mocksource_str);
		logger.info(
				mocksource_mirror.toJsonFormat()
		);
		
	}
	
	
	
}
